/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.deployment.helper;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.Artifact;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Defines management of associations.
 * 
 * @author Gilles Cannenterre
 * @since 22 janv. 07
 */
public class EdgeAssociationHelper extends com.cea.papyrus.diagram.classdiagram.helper.EdgeAssociationHelper {

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#canConnect(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param target 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canConnect(Element source,Element target){
		boolean isConnected = super.canConnect(source, target);
		if ( (isConnected == false) && ((source instanceof Artifact) || (target instanceof Artifact)) ){
			return true;
		}
		return isConnected;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement source){
		boolean canCreate = super.canCreateGraphElement(source);
		/*Element eltSource=null;
		if (source instanceof Diagram ){
			if (((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement();
				if ( (canCreate == false) && (eltSource instanceof  Artifact) ){
					return true;
				}
			}
		}
		if (source.getSemanticModel()!=null ){
			if (((Uml1SemanticModelBridge)source.getSemanticModel()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)source.getSemanticModel()).getElement();
				if ( (canCreate == false) && (eltSource instanceof  Artifact) ){
					return true;
				}
			}
		}*/
		return canCreate;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createGraphNodeWithSemantic(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlChild 
	 * @param size 
	 * @param location 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public GraphElement createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size){
		GraphElement ge = super.createGraphElement(umlChild, parent, location, size);
		
		// Role of AssociationEnd number2
		((GraphNode)ge.getContained().get(2)).setIsVisible(true);

		// Multiplicity of AssociationEnd number2
		((GraphNode)ge.getContained().get(3)).setIsVisible(false);
		
		return ge;		
	}
}